package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"log"
)

func main() {
	db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/mygo?charset=utf8")
	if err != nil {
		panic(err)
	}

	result, err := db.Exec("INSERT INTO user_info (username, departname) VALUES (?, ?)", "gopher", 27)
	if err != nil {
		panic(err)
	}
	id, _ := result.LastInsertId()
	fmt.Println(id)

	var departname string
	var username string
	rows, err := db.Query("SELECT username FROM user_info WHERE id = ?", id)
	if err != nil {
		log.Fatal(err)
	}

	for rows.Next() {
		if err := rows.Scan(&departname); err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s is %s\n", rows.Scan(&username), departname)
	}
	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}
}
